package ru.usu.gv.gui.frames;

import ru.usu.gv.gui.frames.styles.GUIStyleUtilities;
import ru.usu.gv.styles.StyleUtilities;
import ru.usu.gv.uicomponents.IUIVertexType;
import ru.usu.gv.uicomponents.UIVertex;
import ru.usu.gv.uicomponents.UIVertexTypeRegistry;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

import java.awt.BorderLayout;
import java.awt.Color;
import java.util.ArrayList;
import java.util.List;

/**
 @author Sergey Pupyrev 
 01.04.2007
 */

public class EditVertexFrame extends JFrame
{
	private UIVertex vertex;
	private List<ISaveCallback> saveCallbacks = new ArrayList<ISaveCallback>();

	public EditVertexFrame(UIVertex vertex)
	{
		this.vertex = vertex;
		setTitle("Редактирование вершины");
		initComponents();
	}

	private void initComponents()
	{
		//SETTINGS
		JPanel settingPanel = new JPanel();
		settingPanel.setLayout(new BoxLayout(settingPanel, BoxLayout.Y_AXIS));

		settingPanel.add(createVertexEditPanel());
		settingPanel.add(createLabelPanel());

		JPanel applyPanel = createSaveCancelPanel();

		getContentPane().add(settingPanel, BorderLayout.NORTH);
		getContentPane().add(applyPanel, BorderLayout.SOUTH);
		pack();
	}

	private JPanel createSaveCancelPanel()
	{
		//SAVINGS
		JPanel applyPanel = new JPanel();

		//apply
		JButton applyButton = new JButton("Применить");
		applyButton.addActionListener(GUIStyleUtilities.createApplyListener(this, saveCallbacks));
		applyPanel.add(applyButton);

		//cancel
		JButton cancelButton = new JButton("Отмена");
		cancelButton.addActionListener(GUIStyleUtilities.createCancelListener(this));
		applyPanel.add(cancelButton);

		return applyPanel;
	}

	private JPanel createLabelPanel()
	{
		JPanel labelPanel = new JPanel();
		labelPanel.setBorder(new TitledBorder("Метка"));

		final JTextField labelField = new JTextField(20);
		labelField.setText(vertex.getLabel());
		labelPanel.add(labelField);

		saveCallbacks.add(new ISaveCallback()
		{
			public void doSave()
			{
				vertex.setLabel(labelField.getText());
			}
		});
		return labelPanel;
	}

	private EditVertexPanelBase createVertexEditPanel()
	{
		return new EditVertexPanelBase(saveCallbacks, true)
		{
			@Override
			protected String getVertexColor()
			{
				Color color = vertex.getViewItem().getColor();
				return StyleUtilities.encodeColor(color);
			}

			@Override
			protected double getVertexSize()
			{
				return vertex.getViewItem().getSize();
			}

			@Override
			protected String getVertexTypeId()
			{
				return vertex.getVertexType().getId();
			}

			@Override
			protected void setVertexColor(String encodedColor)
			{
				Color color = StyleUtilities.decodeColor(encodedColor);
				vertex.getViewItem().setColor(color);
			}

			@Override
			protected void setVertexSize(double newSize)
			{
				vertex.getViewItem().setSize(newSize);
				vertex.draw();
			}

			@Override
			protected void setVertexTypeId(String newVertexTypeId)
			{
				IUIVertexType newVertexType = UIVertexTypeRegistry.findVertexType(newVertexTypeId);
				vertex.changeVertexType(newVertexType);
				vertex.draw();
			}
		};
	}
}
